import { Column, Entity, JoinColumn, OneToMany } from 'typeorm'
import { Operate } from './operate.entity'
import { UserRole } from './user-role.entity'

@Entity()
export class User extends Operate {
  @Column({ name: 'nick_name', type: 'varchar', nullable: true, length: 30, comment: '账户昵称' })
  nickName: string

  @Column({ name: 'user_name', type: 'varchar', length: 30, comment: '用户名' })
  userName: string

  @Column({ name: 'password', type: 'varchar', nullable: true, length: 64, comment: '账号密码' })
  password: string

  @Column({
    name: 'status',
    type: 'enum',
    enum: ['1', '2'],
    default: '1',
    comment: '状态：1、可用 2、禁用',
  })
  status: string

  @Column({ name: 'otp_secret', type: 'varchar', length: 100, nullable: true })
  otpSecret: string | null

  @Column({
    name: 'user_gender',
    type: 'enum',
    enum: ['1', '2'],
    comment: '性别: 1,男性 2， 女性',
    nullable: true,
  })
  userGender: string

  @Column({
    name: 'user_phone',
    type: 'varchar',
    length: 20,
    comment: '电话',
    nullable: true,
  })
  userPhone: string

  @Column({
    name: 'user_email',
    type: 'varchar',
    length: 30,
    comment: '电子邮箱',
    nullable: true,
  })
  userEmail: string

  @Column({
    name: 'last_login_time',
    type: 'datetime',
    comment: '最后登录时间',
    nullable: true,
  })
  lastLoginTime: string

  @Column({
    name: 'last_login_ip',
    type: 'varchar',
    length: 50,
    comment: '登录ip',
    nullable: true,
  })
  lastLoginIp: string

  @OneToMany(() => UserRole, (userRole) => userRole.user, {
    cascade: true,
  })
  @JoinColumn({ name: 'id', referencedColumnName: 'role_id', foreignKeyConstraintName: 'role_id' })
  roles: UserRole[]
}
